home *** CD-ROM | disk | FTP | other *** search
/ Gigarom 1 / Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso / FILES / DEV / C-H / CHexDmpDA.cpt / Hex Dump DA / goto.HexDumpDA.c < prev    next >
C/C++ Source or Header  |  1988-06-14  |  2KB  |  138 lines

  1. #include <DialogMgr.h>
  2.  
  3. #include "HexDump.h"
  4.  
  5. static long         gotoPoint;
  6. static DialogPtr     dp;
  7.  
  8. extern long         sBegin;
  9. extern long            fileSize;
  10.  
  11. doGoto()
  12. {
  13.     if (Conduct_Goto_Dialog()) {
  14.         New_Select(gotoPoint, gotoPoint+1);
  15.         Show_Point(gotoPoint);
  16.     }
  17. }
  18.  
  19. /* Goto Dialog Items: */
  20. #define OK             1
  21. #define CANCEL         2
  22. #define GOTOSTRING     3
  23.  
  24. pascal Boolean Hex_Digit_Filter(theDialog, theEvent, itemHit)
  25. DialogPtr theDialog;
  26. EventRecord *theEvent;
  27. int *itemHit;
  28. {
  29.     char theChar;
  30.     
  31.     if (theEvent->what==keyDown) {
  32.         theChar = theEvent->message & charCodeMask;
  33.         if ((theChar==0x03) || (theChar==0x0D)) {
  34.             *itemHit = 1;
  35.             return true;
  36.         }
  37.         else if (((theChar>='0') && (theChar<='9')) ||
  38.                 ((theChar>='a') && (theChar<='f')) ||
  39.                 ((theChar>='A') && (theChar<='F')) ||
  40.                 (theChar == 0x09) ||
  41.                 (theChar == 0x08))
  42.             return false;
  43.         else {
  44.             SysBeep(6);
  45.             return true;
  46.         }
  47.     }
  48.     return false;
  49. }
  50.  
  51. Conduct_Goto_Dialog()
  52. {
  53.     int item;
  54.     Str255 s;
  55.     Handle stringH;
  56.     Rect box;
  57.     int type;
  58.     
  59.     dp = GetNewDialog( OwnedResourceID(GOTO_DIALOG), 0L, -1L );    
  60.     Setup_Dialog();
  61.     do {                                    /* until data valid */
  62.         for (;;) {
  63.             ModalDialog( Hex_Digit_Filter, &item );
  64.             if (item==CANCEL) {
  65.                 DisposDialog(dp);
  66.                 return 0;
  67.             }
  68.             if (item==OK) break;
  69.         }
  70.         GetDItem( dp, GOTOSTRING, &type, &stringH, &box );
  71.         GetIText( stringH, s );
  72.     } while (!validValue(s,&gotoPoint));
  73.     DisposDialog(dp);
  74.     return 1;
  75. }
  76.  
  77. Setup_Dialog()
  78. {
  79.     Str255 s;
  80.     int Stype;
  81.     Rect Sbox;
  82.     Handle Sitem;
  83.     
  84.     GetDItem(dp, GOTOSTRING, &Stype, &Sitem, &Sbox);
  85.     Num_To_Hex(sBegin, s);
  86.     SetIText(Sitem, s);
  87.     SelIText(dp, GOTOSTRING, 0, s[0]);
  88. }
  89.  
  90. static
  91. validValue(s,v)
  92. register char *s;
  93. register long *v;
  94. {
  95.     register int i;
  96.     
  97.     *v = 0;
  98.     for (i=*s++; i>0; --i) {
  99.         *v *= 16;
  100.         *v += hexval(*s++);
  101.     }
  102.     if (*v<fileSize) return 1;
  103.     ErrorStr("\pAddress is beyond end of file.");
  104.     return 0;
  105. }
  106.  
  107. static
  108. hexval(c)
  109. register char c;
  110. {
  111.     if ((c>='0')&&(c<='9')) return c-'0';
  112.     if ((c>='a')&&(c<='f')) return c-'a'+10;
  113.     if ((c>='A')&&(c<='F')) return c-'a'+10;
  114.     DebugStr("\phexval failed");
  115. }
  116.  
  117. extern char hexChar[];
  118.  
  119. Num_To_Hex(n,s)
  120. register long n;
  121. register char *s;
  122. {
  123.     register int shift = 28;
  124.     register long m;
  125.     register char *count = s++;
  126.     register leading = true;
  127.     
  128.     *count = 0;
  129.     for (shift=28; shift>=0; shift -=4) {
  130.         m = n >>shift;
  131.         if ((m>0) || ((m==0) && ((shift==0)||(!leading)))) {
  132.             leading = false;
  133.             (*count)++;
  134.             *s++ = hexChar[m&0x0000000F];
  135.         }
  136.     }
  137. }
  138.